// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    selected: "0",
    display: 1,
    pagesize: 1,
    //待使用
    wait_used: [],
    //已过期
    overdue: [],
    used: [],
    underway: [],
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    index: "", //索引值
    trading_money:0,
    balanceLang: '余额',
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getUsable();
    this.getNetData('');
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    try {
      let value = wx.getStorageSync('yz_basic_info');
      if (value) {
        value = value.balance || "余额";
        this.setData({
          balanceLang: value
        });
        wx.setNavigationBarTitle({
          title: value
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {},

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.recharge.page");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            value: resData.data.member_usable,
            every_value: resData.data.recharge_rate_money,
            transform_value: resData.data.recharge_rate_love,
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 标签切换的时候触发
  onChange(val) {
    let index;
    if (val.detail.index == 0) {
      index = val.detail.index = "";
      this.setData({
        selected: ""
      });
    } else {
      index = val.detail.index -= 1;
      this.setData({
        index
      });
    }
    this.getNetData(index);
  },

  //发起http请求
  getNetData(status, own = '') {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Trading.Controllers.trading.trading-center");
    urlStr += "&status=" + status;
    urlStr += "&own=" + own;
    urlStr += "&page=1";
    console.log(urlStr, 'struel');
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          var myData = res.data.data;
          this.setData({
            total_page: res.data.data.total
          });

          if (status == '' && typeof status == 'string') {
            this.setData({
              underway: [],
              done: [],
              own: [],
              wait_used: myData
            });

          } else if (status == 1) {
            this.setData({
              wait_used: [],
              underway: [],
              own: [],
              done: myData
            });
          } else if (status == 2) {
            this.setData({
              wait_used: [],
              underway: [],
              done: [],
              own: myData
            });
          } else if (status == 0) {
            this.setData({
              wait_used: [],
              done: [],
              own: [],
              underway: myData
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  revoke(val) {
    let id = val.currentTarget.dataset.id;
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Trading.Controllers.trading.revoke");
    urlStr += "&id=" + id;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          // MessageBox.alert(res.msg).then(action => {
          //   this.$router.go(0);
          // });
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
          setTimeout(() => {
            if (this.data.selected == "0") {
              this.getNetData(this.data.index);
            }
          }, 1000);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  purchase(val) {
    let id = val.currentTarget.dataset.id;
    let amount = val.currentTarget.dataset.amount;
    wx.showModal({
      content: `点击确定将扣除${(Number(this.data.trading_money) * Number(amount)).toFixed(2)}${this.data.balanceLang},操作不可逆转，请咨询核对是否确认兑换`,
      success:(resData)=>{
        console.log(resData)
        if(resData.confirm){
          let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Trading.Controllers.trading.purchase");
          urlStr += "&id=" + id;
          app._getNetWork({
            url: urlStr,
            success: (res) => {
              if (res.data.result == 1) {
                wx.showToast({
                  title: res.data.msg,
                  icon: "none"
                });
                setTimeout(() => {
                  if (this.data.selected == "0") {
                    this.getNetData("");
                  } else {
                    this.getNetData("0");
                  }
                }, 1000);
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "none"
                });
              }
            },
            fail: function(res) {
              console.log(res);
            }
          });
        }
      },
      fail:(err)=>{
        //取消按钮
      }
    })
    
  },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            love_name: res.data.data.love_name,
            trading_money:res.data.data.trading_set.trading_money
          });
          wx.setNavigationBarTitle({
            title: res.data.data.love_name ? (res.data.data.love_name + '交易中心') : '交易中心'
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });

  }
});
